package com.intertid.sz.web;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.intertid.sz.WS.Pagination;
import com.intertid.sz.WS.szxwC;
import com.intertid.sz.WS.szxwWS;
import com.intertid.sz.dao.SzXwDAO;
import com.intertid.util.AppConfig;
import com.intertid.util.JSONUtil;


@Controller
@Scope("prototype")
@RequestMapping("/szxm")
public class SzxwWeb {
	
	@Autowired
	SzXwDAO sd;
	
	@RequestMapping("/lieb")
	@ResponseBody
	public Pagination<szxwWS> name(HttpServletRequest request,HttpServletResponse response){
		
		 String chanIds="";
		 if(StringUtils.isNotBlank(request.getParameter("chanIds"))){
			 chanIds=request.getParameter("chanIds");
			 
		 }
		
		 
	 int status=0;
		 if(StringUtils.isNotBlank(request.getParameter("status"))){
			 status=Integer.parseInt(request.getParameter("status"));
			 
		 }
		 
		 
		 String token="";
		 if(StringUtils.isNotBlank(request.getParameter("token"))){
			 token=request.getParameter("token");
			 
		 }
		 
		 int pageSize=50;
			if(StringUtils.isNotBlank(request.getParameter("pageSize"))){
		    	   
				pageSize=Integer.parseInt(request.getParameter("pageSize"));
		       }
			int pageIndex=1;
			
			if(StringUtils.isNotBlank(request.getParameter("pageIndex"))){
		    	   
				pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
		       }
		 
		 
		  String ipAddress = request.getHeader("x-forwarded-for");
	        
	        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getHeader("Proxy-Client-IP");
	        }
	        if (ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getHeader("WL-Proxy-Client-IP");
	        }
	        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getRemoteAddr();
	            
	            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
	                //根据网卡获取本机配置的IP地址
	                InetAddress inetAddress = null;
	                try {
	                    inetAddress = InetAddress.getLocalHost();
	                } catch (UnknownHostException e) {
	                    e.printStackTrace();
	                }
	                ipAddress = inetAddress.getHostAddress();
	            }
	        }
	        
	        //对于通过多个代理的情况，第一个IP为客户端真实的IP地址，多个IP按照','分割
	        if(null != ipAddress && ipAddress.length() > 15){
	            //"***.***.***.***".length() = 15
	            if(ipAddress.indexOf(",") > 0){
	                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
	            }
	        }
	        String s=AppConfig.getProperty("token"); 
	        
	   

	        
	        
	        Pagination<szxwWS> page =new Pagination<szxwWS>();
	        if(s.equals(token)){
	        	  if(status==1){
	      	        String xh= sd.XunH(chanIds, "");
	      	        String xhs=xh.substring(1);
	      	        System.out.println(xhs);
	      	      page=sd.SxD(xhs, status, ipAddress, pageSize, pageIndex);
	        	  }else{
	        	page=sd.SxD(chanIds, status, ipAddress, pageSize, pageIndex);
	        	  }
	        	  return page;
	        }else {
	        	
	        page.setHk("token值不对请重新输入");
				
	        return page;
			}
	        

	        
	        
	     
 		
	}
	
	@RequestMapping("/content")
	@ResponseBody
	public Pagination<szxwC> Content(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Pagination<szxwC> page =new Pagination<szxwC>();
		 szxwC sW=new szxwC();
		String docId="";
		 if(StringUtils.isNotBlank(request.getParameter("docId"))){
			 docId=request.getParameter("docId");
			 
		 }
		 
		 String token="";
		 if(StringUtils.isNotBlank(request.getParameter("token"))){
			 token=request.getParameter("token");
			 
		 }
		 
		 String ipAddress = request.getHeader("x-forwarded-for");
	        
	        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getHeader("Proxy-Client-IP");
	        }
	        if (ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getHeader("WL-Proxy-Client-IP");
	        }
	        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	            ipAddress = request.getRemoteAddr();
	            
	            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
	                //根据网卡获取本机配置的IP地址
	                InetAddress inetAddress = null;
	                try {
	                    inetAddress = InetAddress.getLocalHost();
	                } catch (UnknownHostException e) {
	                    e.printStackTrace();
	                }
	                ipAddress = inetAddress.getHostAddress();
	            }
	        }
	        
	        //对于通过多个代理的情况，第一个IP为客户端真实的IP地址，多个IP按照','分割
	        if(null != ipAddress && ipAddress.length() > 15){
	            //"***.***.***.***".length() = 15
	            if(ipAddress.indexOf(",") > 0){
	                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
	            }
	        }
	        String s=AppConfig.getProperty("token"); 
	        
	        if(s.equals(token)){
	        	
	        	page=sd.conCs(docId,ipAddress);
	        	  return page;
	         
	        	
	        }else{
	        	
	        	page.setHk("token值不对请重新输入");
	        
	        	  return page;
			}

		 
	
	}

	
	 
	
}
